package com.xxw.yygh.user.controller.user;



import com.xxw.yygh.common.result.R;
import com.xxw.yygh.common.utils.JwtHelper;
import com.xxw.yygh.enums.AuthStatusEnum;
import com.xxw.yygh.model.user.UserInfo;
import com.xxw.yygh.vo.user.LoginVo;
import com.xxw.yygh.vo.user.UserAuthVo;
import com.xxw.yygh.user.service.UserInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;

/**
 * 用户表(UserInfo)表控制层
 *
 * @author makejava
 * @since 2023-03-01 11:28:54
 */
@RestController
@RequestMapping("/user/userinfo")
public class UserInfoController{

    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo){
        Map<String,Object> map=userInfoService.login(loginVo);
        return R.ok().data(map);
    }

    //获取用户信息
    //@RequestHeader 获取请求头信息 前端发送token到请求头,后端使用这个注解接收
    @GetMapping("/info")
    public R getUserInfo(@RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        UserInfo byId = userInfoService.getUserInfo(userId);
        return R.ok().data("user",byId);
    }

    //保存用户信息
    @PutMapping("/save")
    public R save(@RequestHeader String token, UserAuthVo userAuthVo){
        Long userId = JwtHelper.getUserId(token);
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setName(userAuthVo.getName());
        userInfo.setCertificatesType(userAuthVo.getCertificatesType());
        userInfo.setCertificatesUrl(userAuthVo.getCertificatesUrl());
        userInfo.setCertificatesNo(userAuthVo.getCertificatesNo());
        userInfo.setAuthStatus(AuthStatusEnum.AUTH_RUN.getStatus());

        userInfoService.updateById(userInfo);
        return R.ok();
    }

}

